/**
* @file datatype.h
* @brief data type define
* @details 
* @author huangbin
* @version V1.0.0
* @date 2025-04-20
* @copyright All documents are copyrighted by Shenzhen Xinlongwei Technology Co., LTD.
*            They may not be used for commercial purposes without permission
*/

#ifndef		__DATETYPE__
#define		__DATETYPE__
#include 	<stdio.h>
#include 	<stdlib.h>
#include 	<string.h>
#include 	<stdint.h>
#include	<ctype.h>
#include	<stdbool.h>

#ifdef __cplusplus
 extern "C" {
#endif /* __cplusplus */
//=====================================================================
/* 常用常数定义*/
#undef TRUE
#undef FALSE
#ifndef TRUE
	#define TRUE			1
	#define FALSE      		0
#endif

#define true			1
#define false			0

#undef NULL
#ifndef	NULL
	#define NULL        	((void *)0)
#endif

#define	ON				TRUE
#define OFF        		FALSE
#define HIGH       		TRUE
#define LOW        		FALSE
#define ENABLED    		TRUE
#define DISABLED   		FALSE
	
#define STR_NULL_ID        0xFFFFFFFF      //无效字符串id

#define INVALID_RES_ID		0xFFFFFFFF		//无效的资源ID
	

/* 一般性常用的 MACRO */
#define	MASK_BIT0				((UINT8)0x01)
#define	MASK_BIT1				((UINT8)0x02)
#define	MASK_BIT2				((UINT8)0x04)
#define	MASK_BIT3				((UINT8)0x08)
#define	MASK_BIT4				((UINT8)0x10)
#define	MASK_BIT5				((UINT8)0x20)
#define	MASK_BIT6				((UINT8)0x40)
#define	MASK_BIT7				((UINT8)0x80)
#define	MASK_BIT8				0x0100
#define	MASK_BIT9				0x0200
#define	MASK_BIT10				0x0400
#define	MASK_BIT11				0x0800
#define	MASK_BIT12				0x1000
#define	MASK_BIT13				0x2000
#define	MASK_BIT14				0x4000
#define	MASK_BIT15				0x8000
#define	MASK_BIT16				0x00010000UL
#define	MASK_BIT17				0x00020000UL
#define	MASK_BIT18				0x00040000UL
#define	MASK_BIT19				0x00080000UL
#define	MASK_BIT20				0x00100000UL
#define	MASK_BIT21				0x00200000UL
#define	MASK_BIT22				0x00400000UL
#define	MASK_BIT23				0x00800000UL
#define	MASK_BIT24				0x01000000UL
#define	MASK_BIT25				0x02000000UL
#define	MASK_BIT26				0x04000000UL
#define	MASK_BIT27				0x08000000UL
#define	MASK_BIT28				0x10000000UL
#define	MASK_BIT29				0x20000000UL
#define	MASK_BIT30				0x40000000UL
#define	MASK_BIT31				0x80000000UL

/* 取最小值,最大值 */
#undef MIN
#undef MAX
#undef ABS
#define	MIN(a, b)			(((a) < (b)) ? (a) : (b))
#define	MAX(a, b)			(((a) > (b)) ? (a) : (b))
#define ABS(X)				((X) > 0 ? (X) : -(X))

/*=====  结构定义 ===========*/
//8 bit 结构
typedef struct Bit16{

    uint16_t   d0			: 1;            /* bit 0 */
    uint16_t   d1			: 1;            /* bit 1 */
    uint16_t   d2			: 1;            /* bit 2 */
    uint16_t   d3			: 1;            /* bit 3 */
    uint16_t   d4			: 1;            /* bit 4 */
    uint16_t   d5			: 1;            /* bit 5 */
    uint16_t   d6			: 1;            /* bit 6 */
    uint16_t   d7			: 1;            /* bit 7 */
    uint16_t   d8			: 1;            /* bit 9 */
    uint16_t   d9			: 1;            /* bit 10 */
    uint16_t   d10		    : 1;            /* bit 11 */
    uint16_t   d11		    : 1;            /* bit 12 */
    uint16_t   d12		    : 1;            /* bit 13 */
    uint16_t   d13		    : 1;            /* bit 14 */
    uint16_t   d14		    : 1;            /* bit 15 */
    uint16_t   d15		    : 1;            /* bit 16 */
} Bit16;

//Byte , bit 的联合体
typedef union TUnionWord{

    uint16_t  word;                   /* word */
    Bit16   bit;                    /* 16 bit 的结构 */
    struct{
    	uint16_t	low			: 8;
    	uint16_t	high		: 8;
	} byte;
} UnionWord;

#define    	WPARAM        	uint32_t
#define    	LPARAM        	uint32_t
 /**窗口处理函数指针 */
typedef uint32_t	(* WNDPROC)(WPARAM wParam, LPARAM lParam);

/**
* @brief 串口接收完成，转移数据的函数
* @param[in] pbuf buffer for receive data
* @param[in] len data length
* @retval true ok
*/  
typedef bool (* UART_COPY_DATA)(uint8_t *pbuf,uint16_t len);

    
#define LOBYTE(word)           ((BYTE)((word) & 0x00FF))		                        ///<返回word的低byte
#define HIBYTE(word)           ((BYTE)(((WORD)(word) >> 8) & 0xFF))						///<返回word的高byte
#define MAKEWORD(high,low)      ((WORD)(((BYTE)(low)) | (((WORD)((BYTE)(high))) << 8)))	///<组合两个byte为word
#define LOWORD(l)           ((WORD)(DWORD)(l))						                    ///<返回long的低word
#define HIWORD(l)           ((WORD)((((DWORD)(l)) >> 16) & 0xFFFF))	                    ///<返回long的高word
#define LOSWORD(l)          ((SWORD)(DWORD)(l))						                    ///<Returns the low signed word of the double word
#define HISWORD(l)          ((SWORD)((((DWORD)(l)) >> 16) & 0xFFFF))		            ///<Returns the high signed word of the double word
#define MAKELONG(high,low) ((DWORD)(((WORD)(low)) | (((DWORD)((WORD)(high))) << 16)))   ///<组合两个word为Dword

/**求一个数组的元素数量 */
#define SIZEOF(x) 			(sizeof(x)/sizeof(x[0]))

/**计算结构中成员的偏移 */	
#define	STD_OFFSETOF(Struct, member)		((TWord)(&(((Struct *)0)->member)))

/**计算出结构中某一个成员的size */	
#define	STD_SIZEOF(Struct, member)			sizeof(((Struct *)0x00000000L)->member)

/**DISABLE_INTERRUPT和ENABLE_INTERRUPT必须配对使用，避免以外的开关中断*/
/**这是一个过渡性的方案，用宏__IRQ_SAFE也是一种方法，但有移植性依赖
*这里不用do{}while(0)结构，它限制了局部变量的访问范围
*现在的限制条件是：函数不能定义相同的变量：__tInterrupt_Status__
*/
/**全局中断disable*/	
#define DISABLE_INTERRUPT()			uint32_t __tInterrupt_Status__ = __get_PRIMASK(); __disable_irq()
                                    

/**全局中断enable */	
#define ENABLE_INTERRUPT()			__set_PRIMASK(__tInterrupt_Status__)

#define PRAGMA_PACK __packed

//////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
/************************END OF FILE*************************************/

